package com.voygern.task1.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.voygern.task1.model.User;
import com.voygern.task1.service.UserService;


@Controller
public class HomePageController {
	@Autowired
	private UserService service;
	@RequestMapping("/home")
	public String home(ModelMap model)
	{
		model.addAttribute("message","Hello World");
		List<User> users = service.queryAllUsers();
		model.addAttribute("users", users);
		return "getDate";
	}
	@RequestMapping(value="/user.do",params="method=add")
	public String addUser(User user)
	{
		Date now = new Date();
		System.out.println(now.getTime());
		user.setCreate_at(now.getTime());
		user.setUpdate_at(now.getTime());
		service.add(user);
		return "redirect:/home";
	}
	@RequestMapping(value="/user.do",params="method=delete")
	public String deleteUser(int id)
	{
		service.deleteUser(id);
		return "redirect:/home";
	}
//	@RequestMapping(value="/getDate",method=RequestMethod.GET)
//	@ResponseBody
//	public Map<String,Object> getDate(HttpServletResponse resp)
//	{
//		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
//		String dateTime = tempDate.format(new Date());
//		Map<String,Object> map = new HashMap<String, Object>();
//		map.put("dateTime", dateTime);
//		return map;
//	}
	//
	@RequestMapping(value="/getDate",method=RequestMethod.GET)
	@ResponseBody
	public Map<String,Object> getDate()
	{
		SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd");
		String dateTime = tempDate.format(new Date());
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("dateTime", dateTime);
		return map;
	}
}
